﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SupermarketSalesModel;
using System.Xml.Serialization;
using System.IO;

namespace SupermarketSalesController
{
    [Serializable]
    public class ProductsDB
    {
        private List<Product> productList = new List<Product>();
        public Product this[int i]
        {
            get
            {
                return productList[i];
            }
            set
            {
                productList[i] = value;
            }
        }

        public List<Product> Values
        {
            get { return productList; }
        }

        public void Add(Product p)
        {
            productList.Add(p);
        }
        public int Count
        {
            get { return productList.Count; }
        }
        public void Delete(int i)
        {
            productList[i].Status = 0;
        }

        public ProductsDB() { }
    }

    [Serializable]
    public class ClientsDB{

        private List<Client> clientList = new List<Client>();
        public Client this[int i]
        {
            get
            {
                return clientList[i];
            }
            set
            {
                clientList[i] = value;
            }
        }

        public List<Client> Values
        {
            get { return clientList; }
        }

        public void Add(Client p)
        {
            clientList.Add(p);
        }
        public int Count
        {
            get { return clientList.Count; }
        }
        public void Delete(int i)
        {
            clientList[i].Status = 0;
        }

        public ClientsDB() { }

    }

    [Serializable]
    public class SaleDB
    {

        private List<Sale> saleList = new List<Sale>();
        public Sale this[int i]
        {
            get
            {
                return saleList[i];
            }
            set
            {
                saleList[i] = value;
            }
        }

        public List<Sale> Values
        {
            get { return saleList; }
        }

        public void Add(Sale p)
        {
            saleList.Add(p);
        }
        public int Count
        {
            get { return saleList.Count; }
        }
        public void Delete(int i)
        {
            saleList[i].Flag = 0;
        }

        public SaleDB() { }
    }

    [Serializable]
    public class CashRegisterDB
    {
        private List<CashRegister> crList = new List<CashRegister>();
        public CashRegister this[int i]
        {
            get
            {
                return crList[i];
            }
            set
            {
                crList[i] = value;
            }
        }

        public List<CashRegister> Values
        {
            get { return crList; }
        }

        public void Add(CashRegister cr)
        {
            crList.Add(cr);
        }
        public int Count
        {
            get { return crList.Count; }
        }
        public void Delete(int i)
        {
            crList[i].Status = 0;
        }

        public CashRegisterDB() { }
    }

    [Serializable]
    public class SalesclerkDB
    {

        private List<Salesclerk> scList = new List<Salesclerk>();
        public Salesclerk this[int i]
        {
            get
            {
                return scList[i];
            }
            set
            {
                scList[i] = value;
            }
        }

        public List<Salesclerk> Values
        {
            get { return scList; }
        }

        public void Add(Salesclerk sc)
        {
            scList.Add(sc);
        }
        public int Count
        {
            get { return scList.Count; }
        }
        public void Delete(int i)
        {
            scList[i].Status = 0;
        }

        public SalesclerkDB() { }

    }

    [Serializable]
    public class CreditNoteDB
    {

        private List<CreditNote> cnList = new List<CreditNote>();
        public CreditNote this[int i]
        {
            get
            {
                return cnList[i];
            }
            set
            {
                cnList[i] = value;
            }
        }

        public List<CreditNote> Values
        {
            get { return cnList; }
        }

        public void Add(CreditNote p)
        {
            cnList.Add(p);
        }
        public int Count
        {
            get { return cnList.Count; }
        }
        public void Delete(int i)
        {
            cnList[i].Flag = 0;
        }

        public CreditNoteDB() { }
    }



    public class SalesManager
    {
        //private static List<Product> productList = new List<Product>();
        static ProductsDB products = new ProductsDB();
        //private static List<Client> clientList = new List<Client>();
        static ClientsDB clients = new ClientsDB();
        //private static List<CashRegister> crList = new List<CashRegister>();
        static CashRegisterDB cashRegisters = new CashRegisterDB();
        //private static List<Sale> saleList = new List<Sale>();
        static SaleDB sales = new SaleDB();
        //private static List<Salesclerk> salesclerkList = new List<Salesclerk>();
        static SalesclerkDB salesClerks = new SalesclerkDB();
        //private static List<CreditNote> cnList = new List<CreditNote>();
        static CreditNoteDB creditNotes = new CreditNoteDB();

        /*SERIALIZACION DE CLIENTES*/
        public static void PersistClientsDB()
        {
            XmlSerializer writer = new XmlSerializer(typeof(ClientsDB));
            TextWriter file = new StreamWriter("ClientsDB.xml");
            writer.Serialize(file, clients);
            file.Close();
        }
        public static void ReloadClientsDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ClientsDB));
            FileStream fs = new FileStream("ClientsDB.xml", FileMode.Open);
            clients = (ClientsDB)serializer.Deserialize(fs);
            fs.Close();

        }

        /*SERIALIZACION DE LA VENTA*/
        public static void PersistSaleDB()
        {          
            XmlSerializer writer = new XmlSerializer(typeof(SaleDB));
            TextWriter file = new StreamWriter("SaleDB.xml");
            writer.Serialize(file, sales);
            file.Close();
        }
        public static void ReloadSaleDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SaleDB));
            FileStream fs = new FileStream("SaleDB.xml", FileMode.Open);
            sales = (SaleDB)serializer.Deserialize(fs);
            fs.Close();

        }

        /*SERIALIZACION DE PRODUCTOS*/
        public static void PersistProductsDB()
        {
            XmlSerializer writer = new XmlSerializer(typeof(ProductsDB));
            TextWriter file = new StreamWriter("ProductsDB.xml");
            writer.Serialize(file, products);
            file.Close();
        }
        public static void ReloadProductsDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ProductsDB));
            FileStream fs = new FileStream("ProductsDB.xml", FileMode.Open);
            products = (ProductsDB)serializer.Deserialize(fs);
            fs.Close();

        }


        /*SERIALIZACION DE CAJA*/
        public static void PersistCashRegisterDB()
        {
            XmlSerializer writer = new XmlSerializer(typeof(CashRegisterDB));
            TextWriter file = new StreamWriter("CashRegisterDB.xml");
            writer.Serialize(file, cashRegisters);
            file.Close();
        }
        public static void ReloadCashRegisterDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CashRegisterDB));
            FileStream fs = new FileStream("CashRegisterDB.xml", FileMode.Open);
            cashRegisters = (CashRegisterDB)serializer.Deserialize(fs);
            fs.Close();

        }

        /*SERIALIZACION DE CAJERO*/
        public static void PersistClerkDB()
        {
            XmlSerializer writer = new XmlSerializer(typeof(SalesclerkDB));
            TextWriter file = new StreamWriter("SalesclerkDB.xml");
            writer.Serialize(file, salesClerks);
            file.Close();
        }
        public static void ReloadClerkDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SalesclerkDB));
            FileStream fs = new FileStream("SalesclerkDB.xml", FileMode.Open);
            salesClerks = (SalesclerkDB)serializer.Deserialize(fs);
            fs.Close();
        }

        /*SERIALIZACION DE NOTA DE CREDITO*/
        public static void PersistCreditNoteDB()
        {
            XmlSerializer writer = new XmlSerializer(typeof(CreditNoteDB));
            TextWriter file = new StreamWriter("CreditNoteDB.xml");
            writer.Serialize(file, creditNotes);
            file.Close();
        }
        public static void ReloadCreditNoteDB()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CreditNoteDB));
            FileStream fs = new FileStream("CreditNoteDB.xml", FileMode.Open);
            creditNotes = (CreditNoteDB)serializer.Deserialize(fs);
            fs.Close();

        }


        /*PRODUCTS*/

        public static void addProduct(Product p)
        {
            products.Add(p);
        }
        
        public static Product getProductbyId(int id){
            for (int i=0; i<products.Count; i++){
                if (products[i].Id == id){
                    return products[i];
                }
            }
            return null;
        }

        public static List<Product> getAllProducts()
        {
            return products.Values;
        }


        public static void deleteProduct(int id)
        {
            int i;
            for (i = 0; i < products.Count; i++)
            {
                if (products[i].Id == id)
                {
                    break;  
                }
            }            
            if (id >= products.Count) products.Delete(i);
        }

        /*CLIENTS*/

        public static void addClient(Client c)
        {
            clients.Add(c);
        }

        public static Client getClientbyIdCard(String idCard)
        {
            for (int i = 0; i < clients.Count; i++)
            {
                if (clients[i].IdCard.CompareTo(idCard) == 0)
                {
                    return clients[i];
                }
            }
            return null;
        }

        public static List<Client> getAllClients()
        {
            return clients.Values;
        }

        public static void deleteClient(String idcardIn)
        {
            int i;
            int enc = 0;
            for (i = 0; i < products.Count; i++)
            {
                if (clients[i].IdCard.Equals(idcardIn))
                {
                    enc = 1;                    
                    break;
                }
            }
            if (enc == 1) products.Delete(i);
        }

        /*CASH REGISTER*/

        public static void addCr(CashRegister cr)
        {
            cashRegisters.Add(cr);
        }

        public static CashRegister getCRbyId(int id)
        {
            for (int i = 0; i < cashRegisters.Count; i++)
            {
                if (cashRegisters[i].Id == id)
                {
                    return cashRegisters[i];
                }
            }
            return null;
        }

        public static List<CashRegister> getAllCashRegisters()
        {
            return cashRegisters.Values;
        }

        public static void deleteCashRegister(int id)
        {
            int i;
            int enc = 0;
            for (i = 0; i < cashRegisters.Count; i++)
            {
                if (cashRegisters[i].Id==id)
                {
                    enc = 1;
                    break;
                }
            }
            if (enc == 1) cashRegisters.Delete(i);
        }

        /*SALES*/

        public static void addSale(Sale s)
        {
            sales.Add(s);
        }

        public static Sale getSaleById(int id)
        {

            for (int i = 0; i < sales.Count; i++)
            {
                if (sales[i].Id == id)
                {
                    return sales[i];
                }
            }
            return null;

        }

        public static List<Sale> getAllSales()
        {
            return sales.Values;
        }

        /*SALESCLERKS*/
     
        public static void addSalesclerk(Salesclerk sc)
        {
            salesClerks.Add(sc);
        }

        public static Salesclerk getSalesclerkbyId(int id)
        {
            for (int i = 0; i < salesClerks.Count; i++)
            {
                if (salesClerks[i].IdClerk == id)
                {
                    return salesClerks[i];
                }
            }
            return null;
        }

        public static List<Salesclerk> getAllSalesclerks()
        {
            return salesClerks.Values;
        }

        public static void deleteSalesclerk(int id)
        {
            int i;
            int enc = 0;
            for (i = 0; i < salesClerks.Count; i++)
            {
                if (salesClerks[i].IdClerk==id)
                {
                    enc = 1;
                    break;
                }
            }
            if (enc == 1) salesClerks.Delete(i);
        }

        /*CREDIT NOTES*/

        public static void addCreditNote(CreditNote cn)
        {
            creditNotes.Add(cn);
        }

        public static CreditNote getCreditNoteById(int id)
        {
            for (int i = 0; i < creditNotes.Count; i++)
            {
                if (creditNotes[i].Id == id)
                {
                    return creditNotes[i];
                }
            }
            return null;
        }

        public static List<CreditNote> getAllCreditNotes()
        {
            return creditNotes.Values;
        }
   
    }
}
